package com.dxj1992.utils;


import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * steam安装包
 *
 * @param
 */
@Data
public class SteamUtils {


    public static JSONObject getCookie(String steamId, String token) {
        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet("http://128.1.131.90/api/getCookie?steam_id=" + steamId + "&token=" + token);
        JSONObject jsonObject = null;
        try {
            //3.执行get请求，相当于在输入地址栏后敲回车键
            response = httpClient.execute(request);
            //4.判断响应状态为200，进行处理
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String s = EntityUtils.toString(response.getEntity());
                jsonObject = JSONObject.parseObject(s);
            }
        } catch (Exception e) {
            //如果返回状态不是200，比如404（页面不存在）等，根据情况做处理，这里略
            System.out.println("返回状态不是200");
        }

        return jsonObject;
    }

    public static JSONObject getGoodsPrice(String steamId, String token, String str) {
        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet(str);
        JSONObject jsonObject = null;
        try {
            //3.执行get请求，相当于在输入地址栏后敲回车键
            response = httpClient.execute(request);
            //4.判断响应状态为200，进行处理
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String s = EntityUtils.toString(response.getEntity());
                jsonObject = JSONObject.parseObject(s);
            }
        } catch (Exception e) {
            //如果返回状态不是200，比如404（页面不存在）等，根据情况做处理，这里略
            System.out.println("返回状态不是200");
        }

        return jsonObject;
    }

    public static JSONObject getGoodsPriceGet() {
        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet("https://steamcommunity.com/market/itemordershistogram?country=HK&language=schinese&currency=1&item_nameid=14962905&two_factor=0");
        JSONObject jsonObject = null;
        try {
            //3.执行get请求，相当于在输入地址栏后敲回车键
            response = httpClient.execute(request);
            //4.判断响应状态为200，进行处理
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String s = EntityUtils.toString(response.getEntity());
                jsonObject = JSONObject.parseObject(s);
            }
        } catch (Exception e) {
            //如果返回状态不是200，比如404（页面不存在）等，根据情况做处理，这里略
            System.out.println("返回状态不是200");
        }

        return jsonObject;
    }

}
